package com.iss.card.webservice;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.iss.card.view.model.ResponseBean;

public class WebServiceUtils {
	private static final String TAG = WebServiceUtils.class.getSimpleName();

	public static Bitmap downLoadPic(String url, int width) {
		System.gc();

		HttpGet httpRequest = new HttpGet(WebServiceManager.imageDownURL + url);
		Log.d(TAG, "downLoadPic: " + httpRequest.getURI());
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response;
		Bitmap bitmap = null;

		try {
			response = (HttpResponse) httpclient.execute(httpRequest);
			HttpEntity entity = response.getEntity();
			BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(
					entity);
			InputStream is = bufferedHttpEntity.getContent();
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inJustDecodeBounds = true;
			// bitmap is null,just to get bitmap's width or height
			bitmap = BitmapFactory.decodeStream(is, null, options);
			int be = options.outWidth / width;
			if (be <= 0) {
				be = 1;
			}
			options.inJustDecodeBounds = false;
			options.inSampleSize = be;
			options.inPreferredConfig = Bitmap.Config.ARGB_8888;
			options.inPurgeable = true;
			options.inInputShareable = true;
			bitmap = BitmapFactory.decodeStream(
					bufferedHttpEntity.getContent(), null, options);
			is.close();
		} catch (ClientProtocolException e) {
			Log.e(TAG, "ClientProtocolException.url is " + url);
			e.printStackTrace();
		} catch (IOException e) {
			Log.e(TAG, "IOException.url is " + url);
			e.printStackTrace();
		} finally {

		}

		return bitmap;
	}

	/**
	 * split value by &.
	 * 
	 * @param result
	 *            info from server.
	 * @return split result.
	 */
	public static String[] getResultToString(Object result) {
		String[] str = null;
		if (result != null && !result.equals("")) {
			str = result.toString().split("&");
		}
		return str;
	}

	/**
	 * parse xml.
	 * 
	 * @param envelope
	 * @param class1
	 * @return corresponding entities
	 */
	public static ResponseBean getObjectResultToBaseInfo(Object envelope,
			Class<? extends BaseModel> class1) {
		try {
			return SaxReadxml.readXml((InputStream) envelope, class1);
		} catch (Exception e) {
			Log.e(TAG, "getObjectResultToBaseInfo:Exception");
			e.printStackTrace();
		}
		return new ResponseBean();
	}

	private final static class SaxReadxml {
		public static ResponseBean readXml(InputStream inStream,
				Class<? extends BaseModel> childInfo) throws Exception {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLContentHandler handler = new XMLContentHandler(childInfo);
			sp.parse(inStream, handler);
			return handler.getModel();
		}
	}

}
